package com.bfdx.testkeyevent

import android.os.Bundle
import android.view.KeyEvent
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import com.bfdx.testkeyevent.ui.theme.TestKeyEventTheme
import com.orhanobut.logger.Logger
import kotlinx.coroutines.launch

class KeyActivity : ComponentActivity() {

    private val keyLogList = mutableStateListOf<KeyLog>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TestKeyEventTheme {
                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background) {
                    val scope = rememberCoroutineScope()
                    val scrollState: LazyListState = rememberLazyListState()
                    LaunchedEffect(keyLogList.size) {
                        scope.launch {
                            scrollState.animateScrollToItem(scrollState.layoutInfo.lastIndex)
                        }
                    }
                    Column(
                        modifier = Modifier.fillMaxSize()
                    ) {
                        Button(
                            modifier = Modifier.fillMaxWidth(),
                            onClick = { keyLogList.clear() }) {
                            Text(text = "清空")
                        }
                        LazyColumn(
                            modifier = Modifier.fillMaxWidth(),
                            state = scrollState,
                            verticalArrangement = Arrangement.Top,
                            horizontalAlignment = Alignment.CenterHorizontally,
                        ) {
                            keyLogList.forEachIndexed { index, keyLog ->
                                item {
                                    Text(text = "${index + 1}. ${formatKeyLog(keyLog)}")
                                }
                            }
                        }
                    }

                }
            }
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        event ?: return super.onKeyDown(keyCode, event)
        if (event.repeatCount > 0) return super.onKeyDown(keyCode, event)
        keyLogList.add(event.toKeyLog())
        Logger.i(keyLogStr)
        return super.onKeyDown(keyCode, event)
    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        event ?: return super.onKeyUp(keyCode, event)
        keyLogList.add(event.toKeyLog())
        Logger.i(keyLogStr)
        return super.onKeyUp(keyCode, event)
    }

    private val keyLogStr: String
        get() = keyLogList.mapIndexed { index, keyLog -> Pair(index, keyLog) }
            .joinToString(separator = "\n") { "${it.first + 1}. ${formatKeyLog(it.second)}" }

}